package com.controller;

import com.bean.User;
import com.dao.JDBC;
import com.service.LoginService;
import com.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/com/controller/ServletLogin")
public class ServletLogin extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        LoginService loginService = new LoginServiceImpl();
        boolean flag= loginService.loginService(user);
        if (flag)
            request.getRequestDispatcher("/LoginSuccess.jsp").forward(request,response);
        else{
            if (username.equals("")||password.equals(""))
                request.setAttribute("ERROR","用户名或密码为空");
            else
                request.setAttribute("ERROR","用户名不存在或密码错误");
            request.getRequestDispatcher("/LoginLoss.jsp").forward(request,response);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
